<?php


// user defined error handling function
function userErrorHandler($errno, $errmsg, $filename, $linenum, $vars) 
{
	// timestamp for the error entry
	$dt = date("Y-m-d H:i:s (T)");
	
	// define an assoc array of error string
	// in reality the only entries we should
	// consider are E_WARNING, E_NOTICE, E_USER_ERROR,
	// E_USER_WARNING and E_USER_NOTICE
	$errortype = array (
		E_ERROR           => "Error",
		E_WARNING         => "Warning",
		E_PARSE           => "Parsing Error",
		E_NOTICE          => "Notice",
		E_CORE_ERROR      => "Core Error",
		E_CORE_WARNING    => "Core Warning",
		E_COMPILE_ERROR   => "Compile Error",
		E_COMPILE_WARNING => "Compile Warning",
		E_USER_ERROR      => "User Error",
		E_USER_WARNING    => "User Warning",
		E_USER_NOTICE     => "User Notice",
		E_STRICT          => "Runtime Notice"
	);
	// set of errors for which a var trace will be saved
	$user_errors = array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_WARNING, E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);
	
	$err = "<errorentry>\n";
	$err .= "\t<datetime>" . $dt . "</datetime>\n";
	$err .= "\t<errornum>" . $errno . "</errornum>\n";
	$err .= "\t<errortype>" . $errortype[$errno] . "</errortype>\n";
	$err .= "\t<errormsg>" . $errmsg . "</errormsg>\n";
	$err .= "\t<scriptname>" . $filename . "</scriptname>\n";
	$err .= "\t<scriptlinenum>" . $linenum . "</scriptlinenum>\n";
	
	if (in_array($errno, $user_errors)) {
		$err .= "\t<vartrace>" . wddx_serialize_value($vars, "Variables") . "</vartrace>\n";
	}
	
	// save to the error log
	// error_log($err, 0, '/var/log/apache/error.log');
	
	ob_start();
		print_r(debug_backtrace());
		$err .= "\t<debugtrace>".ob_get_contents()."</debugtrace>\n";
	ob_end_clean();
	
	$err .= "</errorentry>\n\n";
	// for testing
	// echo $err;
	
	// e-mail me if there is a critical user error
	if (in_array ($errno, $user_errors)) {
		mail(ERRORS_EMAIL, 'RP - Critical Error', $err);
	}
}

?>